package pers.cj.pdos.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.GoodsStock;
import pers.cj.pdos.service.IGoodsStockService;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.UserVo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/goodsStock")
public class GoodsStockController extends BaseController {

    @Autowired
    private IGoodsStockService goodsStockService;

    @ResponseBody
    @RequestMapping("/listByGoods")
    public ResponseData listByGoods (String goodsId) {
        List<GoodsStock> goodsStockList = goodsStockService.listByGoods(goodsId);
        return ResponseData.success(goodsStockList);
    }

    @ResponseBody
    @RequestMapping("/getByGoodsAndHouse")
    public ResponseData getByGoodsAndHouse (String goodsId,String houseId) {
        GoodsStock goodsStock = goodsStockService.getByGoodsAndHouse(goodsId,houseId);
        if (goodsStock == null) {
            return ResponseData.fail("获取失败");
        }
        return ResponseData.success(goodsStock);
    }

    @ResponseBody
    @RequestMapping("/getTotalStockAmount")
    public ResponseData getTotalStockAmount (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BigDecimal total = goodsStockService.getTotalStockAmount(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        return ResponseData.success(total);
    }

    @ResponseBody
    @RequestMapping("/getHighestStockAmountWithGoods")
    public ResponseData getHighestStockAmountWithGoods (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        Map map = goodsStockService.getHighestStockAmountWithGoods(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        if (map==null){
            map = new HashMap();
            map.put("highestStockAmount",0);
            map.put("highestGoods","无");
        }
        return ResponseData.success(map);
    }

    @ResponseBody
    @RequestMapping("/getAmountOrderByGoods")
    public ResponseData getAmountOrderByGoods (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Map> list = goodsStockService.getAmountOrderByGoods(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        return ResponseData.success(list);
    }


    @ResponseBody
    @RequestMapping("/getAmountOrderByStore")
    public ResponseData getAmountOrderByStore (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Map> list = goodsStockService.getAmountOrderByStore(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        return ResponseData.success(list);
    }

    @ResponseBody
    @RequestMapping("/getAmountOrderByHouse")
    public ResponseData getAmountOrderByHouse (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Map> list = goodsStockService.getAmountOrderByHouse(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        return ResponseData.success(list);
    }

    @ResponseBody
    @RequestMapping("/getNumberOrderByHouse")
    public ResponseData getNumberOrderByHouse (String storeId,String houseId) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Map> list = goodsStockService.getNumberOrderByHouse(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
        return ResponseData.success(list);
    }

    @ResponseBody
    @RequestMapping("/getGoodsStockGroupByHouse")
    public ResponseData getGoodsStockGroupByHouse (String goodsId, String storeId,String houseId) {
        if (Utils.isEmpty(goodsId)) {
            return ResponseData.fail("goodsId不能为空");
        }
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Map> list = goodsStockService.getGoodsStockGroupByHouse(company.getId(), goodsId, PdosUtils.getViewOtherStoreId(userVo, storeId),houseId);
        return ResponseData.success(list);
    }
}
